package com.goatrock.media.thisway.domain.adapter.impl;

import com.goatrock.media.thisway.common.constants.ApplicationConstants;
import com.goatrock.media.thisway.common.exceptions.ServiceException;
import com.goatrock.media.thisway.common.model.ApplicationConfig;
import com.goatrock.media.thisway.domain.adapter.ShortcutServiceAdapter;
import java.io.File;
import org.apache.log4j.Logger;

public class ShortcutServiceAdapterImpl implements ShortcutServiceAdapter{
    private Logger logger = Logger.getLogger(ShortcutServiceAdapterImpl.class);
    private final String CREATE_SHORTCUT_VBS_NAME = "\\"+ApplicationConstants.VBSCRIPT_CREATE_SHORTCUT_FILE_NAME;
    private final String REMOVE_SHORTCUT_LOC_VBS_NAME = "\\"+ApplicationConstants.VBSCRIPT_REMOVE_FOLDER_FILE_NAME;

    public void GenerateShortcut(ApplicationConfig appConfig, String shortcutSubfolder, String shortcutFilename, String target) throws ServiceException{
        logger.debug("Inside GenerateShortcut method of the ShortcutServiceAdapterImpl");
        String scriptLocation = new File(appConfig.getScriptsLocation()).getAbsolutePath();

        String shortcutBaseLocation = new File(appConfig.getShortcutBaseLocation()).getAbsolutePath();
        target = new File(target).getAbsolutePath();

        String[] params = new String[6];
        params[0] = "wscript";
        params[1] =  "\"" + scriptLocation + CREATE_SHORTCUT_VBS_NAME + "\"";
        params[2] = "\""+shortcutBaseLocation+"\\"+shortcutSubfolder+"\\\"";
        params[3] = "\"shortcut_"+validateName(shortcutFilename)+".lnk\"";
        params[4] = "\""+target+"\\\"";
        params[5] = "\"shortcut file\""; //b > output.txt";

        Process proc = null;

        try{
            proc = Runtime.getRuntime().exec(params);
            proc.waitFor();

            if(proc.exitValue() != 0){
                logger.error("Error creating shortcut. ReturnCode:"+proc.exitValue());
                throw new ServiceException(ApplicationConstants.RETURN_CODE_CREATE_SHORTCUT_ERROR,
                                            "Error creating shortcut. ReturnCode:"+Integer.toString(proc.exitValue()));
            }
        }catch(Exception e){
            logger.error("Error creating shortcut. ReturnCode:"+proc.exitValue());
            throw new ServiceException(ApplicationConstants.RETURN_CODE_CREATE_SHORTCUT_ERROR,
                                            "Error creating shortcut."+e.getMessage());
        }
    }

    public void RemoveShortcutFolder(ApplicationConfig appConfig, String subFolder) throws ServiceException {
        String scriptLocation = new File(appConfig.getScriptsLocation()).getAbsolutePath();
        String shortcutLocation = new File(appConfig.getShortcutBaseLocation()).getAbsolutePath();

        scriptLocation += this.REMOVE_SHORTCUT_LOC_VBS_NAME;

        String launchRequest = "wscript \""+scriptLocation+"\" \""+shortcutLocation+"\\"+subFolder+"\\\" //b > output.txt";
        Process proc = null;

        try{
            logger.info("Attempting to delete existing shortcut folder.");
            logger.debug("Delete shortcut folder request:"+launchRequest);
            
            proc = Runtime.getRuntime().exec(launchRequest);
            proc.waitFor();

            if(proc.exitValue() != 0){
                logger.error("Error deleting shortcut location. ReturnCode:"+proc.exitValue());
                throw new ServiceException(ApplicationConstants.RETURN_CODE_DELETE_SHORTCUT_LOC_ERROR,
                                            "Error deleting shortcut location. ReturnCode:"+Integer.toString(proc.exitValue()));
            }
            logger.info("Shortcut folder successfully deleted.");
        }catch(Exception e){
            logger.error("Error creating shortcut. ReturnCode:"+proc.exitValue());
            throw new ServiceException(ApplicationConstants.RETURN_CODE_DELETE_SHORTCUT_LOC_ERROR,
                                            "Error deleting shortcut location. ReturnCode:"+e.getMessage());
        }
    }


    private String validateName(String name){
        name = name.replaceAll("\\<|\\>|\\:|\"|\\/|\\|\\||\\?|\\*","-");
        return name;
    }

}
